package cd.cdyb.sms.utils;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.http.client.utils.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTimeUtil {
	private final static Logger log = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	private static SimpleDateFormat sDay = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sDay1 = new SimpleDateFormat("yyyyMMdd");
	private static SimpleDateFormat sDay2 = new SimpleDateFormat("yyyyMMddHHmmss");
	private static String forString="yyyy-MM-dd HH:mm:ss";//格式化样本

	/**获取当前时间*/
	public static String getNowTime() {
		SimpleDateFormat s = new SimpleDateFormat(forString);
		return s.format(new Date());
	}

	/**获取当前时间*/
	public static String getNowTimeyyyyMMdd() {
		return sDay1.format(new Date());
	}

	public static String getDaformatyyyyMMddHHmmss(Date date) {
		return sDay2.format(date.getTime());
	}

	/**获取当前时间往后推61秒*/
	public static String getNowTimeyyyyMMddHHmmss() {
		return sDay2.format(new Date().getTime()+61*1000);
	}

	/**获取当前时间(yyyy-MM-dd)*/
	public static String getNowTimeYMD() {
		return getFormatTime("yyyy-MM-dd");
//		return sDay.format(new Date());
	}


	/**获取两个时间差*/
	public static int getTime(String time1, String time2) {
		SimpleDateFormat s = new SimpleDateFormat(forString);
		int time = 0;
		try {
			time = (int) (s.parse(time2).getTime() - s.parse(time1).getTime()) / 1000;
		} catch (ParseException e) {
			log.error("时间转换出错", e);
		}
		return time;
	}

	/** 日期转字符串 */
	public static Date StringToDateTime(String dateTime) {
		SimpleDateFormat s = new SimpleDateFormat(forString);
		Date date = new Date();
		try {
			date = s.parse(dateTime);
		} catch (ParseException e) {
			log.error("时间转换出错", e);
		}
		return date;
	}

	/** 字符串转日期类型 */
	public static String DateTimeToString(Date d) {
		SimpleDateFormat s = new SimpleDateFormat(forString);
		return s.format(d);
	}

	/** 日期获取当天 */
	public static int getDayOfMonth(Date date) {
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	/** 获取月份的天数 */
	public static int getDayNumOfMonth(Date date) {
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		//判断是不是当月
		Calendar c1 = new GregorianCalendar();
		c1.setTime(new Date());
		if(c.get(Calendar.YEAR)==c1.get(Calendar.YEAR)&&c.get(Calendar.MONTH)==c1.get(Calendar.MONTH)){
			return c1.get(Calendar.DAY_OF_MONTH);
		}
		return c.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**获取和当前的小时差*/
	public static Integer getHour(String startTime){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Date date1=s.parse(startTime);
			Date date2=new Date();

			Long time1=date1.getTime();
			Long time2=date2.getTime();

			Long time=time2-time1;

			Integer baseHour=(int) (time/(60*60*1000));

			if(time%(60*60*1000)>0){
				baseHour++;
			}

			return baseHour;
		}catch(Exception e){
			e.printStackTrace();
		}

		return 0;
	}
	/**
	 * 按照用户输入的分钟数计算时间差
	 * @param startTime  骑行开始时间
	 * @param minuteInput 用户输入的分钟数
	 * @param stopTime 停车时间（秒）
	 * @return
	 */
	public static Integer getMinuteInput(String startTime,Integer minuteInput,Integer stopTime){
		//如果用户没有输入，则直接返回0
		if(minuteInput==null){
			return 0;
		}
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try {
			Date oldTime = s.parse(startTime);
			Date nowTime=new Date();
			long inter=nowTime.getTime()-oldTime.getTime()-stopTime*1000;//两个时间的时间差
			//计算有多少个分钟
			long minuteInputMills=minuteInput*60*1000;//分钟转化为毫秒
			//计算有多少个分钟
			int num=(int) (inter/minuteInputMills);
			if(inter%minuteInputMills>0){
				num++;
			}
			return num;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**获取和当前的小时差（需要减去临时停车的时长）*/
	public static Integer getHour(String startTime,Integer stopTime){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Date date1=s.parse(startTime);
			Date date2=new Date();

			Long time1=date1.getTime();
			Long time2=date2.getTime();

			/**临时停车的时间为秒*/
			Long time=time2-time1-stopTime*1000;

			Integer baseHour=(int) (time/(60*60*1000));

			if(time%(60*60*1000)>0){
				baseHour++;
			}

			return baseHour;
		}catch(Exception e){
			e.printStackTrace();
		}

		return 0;
	}

	/**获取和当前的半小时差*/
	public static Integer getHalfHour(String startTime){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Date date1=s.parse(startTime);
			Date date2=new Date();

			Long time1=date1.getTime();
			Long time2=date2.getTime();

			Long time=time2-time1;

			Integer halfHour=(int) (time/(30*60*1000));

			if(time%(30*60*1000)>0){
				halfHour++;
			}

			return halfHour;
		}catch(Exception e){
			e.printStackTrace();
		}

		return 0;
	}

	/**获取和当前的半小时差*/
	public static Integer getHalfHour(String startTime,Integer stopTime){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Date date1=s.parse(startTime);
			Date date2=new Date();

			Long time1=date1.getTime();
			Long time2=date2.getTime();

			Long time=time2-time1;

			Integer halfHour=(int) (time/(30*60*1000));

			if(time%(30*60*1000)>0){
				halfHour++;
			}

			return halfHour;
		}catch(Exception e){
			e.printStackTrace();
		}

		return 0;
	}

	/**获取和当前的分钟差*/
	public static Integer getMinDiffer(String startTime, String endTime){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Date date1=s.parse(startTime);
			Date date2=s.parse(endTime);

			Long time1=date1.getTime();
			Long time2=date2.getTime();

			Long time=time2-time1;

			Integer minutes=(int) (time/(60*1000));

			if(time%(60*1000)>0){
				minutes++;
			}

			return minutes;
		}catch(Exception e){
			e.printStackTrace();
		}

		return 0;
	}


	/**获取和当前的分钟差*/
	public static Integer getMinDiffer(String startTime){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Date date1=s.parse(startTime);
			Date date2=new Date();

			Long time1=date1.getTime();
			Long time2=date2.getTime();

			Long time=time2-time1;

			Integer minutes=(int) (time/(60*1000));

			if(time%(60*1000)>0){
				minutes++;
			}

			return minutes;
		}catch(Exception e){
			e.printStackTrace();
		}

		return 0;
	}

	/**获取和当前的分钟差*/
	public static Integer getMinDiffer(String startTime,Integer stopTime){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Date date1=s.parse(startTime);
			Date date2=new Date();

			Long time1=date1.getTime();
			Long time2=date2.getTime();

			/**减去临时停车时长*/
			Long time=time2-time1-stopTime*1000;

			Integer minutes=(int) (time/(60*1000));

			if(time%(60*1000)>0){
				minutes++;
			}

			return minutes;
		}catch(Exception e){
			e.printStackTrace();
		}

		return 0;
	}

	/**获取和当前的秒差*/
	public static Integer getSecondDiffer(String startTime){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Date date1=s.parse(startTime);
			Date date2=new Date();

			Long time1=date1.getTime();
			Long time2=date2.getTime();

			Long time=time2-time1;

			return (int)(time/1000);
		}catch(Exception e){
			e.printStackTrace();
		}

		return 0;
	}

	/**指定日期加天数*/
	public static Date addDay(Date dateTime,int days){
		Calendar calender = Calendar.getInstance();
		calender.setTime(dateTime);
		calender.add(Calendar.DAY_OF_MONTH, days);

		return calender.getTime();
	}

	/**指定日期加月份*/
	public static Date addMonth(Date dateTime,int months){
		Calendar calender = Calendar.getInstance();
		calender.setTime(dateTime);
		calender.add(Calendar.MONTH, months);

		return calender.getTime();
	}
	public static Date addOneSecond(){
		Calendar calender = Calendar.getInstance();
		calender.add(Calendar.SECOND, 2);
		return calender.getTime();
	}
	/**
	 * 是否小于今天
	 * @param
	 * @return
	 */
	public static boolean isBeforeToday(String endDate){
		if(endDate==null){
			return false;
		}

		try {
			Long endTime= sDay.parse(endDate).getTime()+24*3600*1000;
            return endTime < System.currentTimeMillis();
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 免押是否有效
	 */
	public static boolean isDepositValid(String endDate){
		if(endDate==null){
			return false;
		}

		try {
			Long endTime= sDay.parse(endDate).getTime()+24*3600*1000;
            return endTime > System.currentTimeMillis();
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 获取服务器截止日期
	 * @param startDate
	 * @return
	 */
	public static String getServiceEndDate(String startDate,Integer type){
		try {
			/**开始日期为空*/
			if(startDate==null){
				startDate=getNowTimeYMD();
			}

			/**开始日期小于当前日期*/
			Long startTime = sDay.parse(startDate).getTime()+24*3600*1000;
			if(startTime<System.currentTimeMillis()){
				startDate=getNowTimeYMD();
			}

			/**在最新的时间上累加*/
			Calendar calender = Calendar.getInstance();
			calender.setTime(sDay.parse(startDate));
			if(type==0){//日
				calender.add(Calendar.DATE, 1);
			}else if(type==1){//周
				calender.add(Calendar.DATE, 7);
			}else if(type==2){//月
				calender.add(Calendar.MONTH, 1);
			}else if(type==3){//季
				calender.add(Calendar.MONTH, 3);
			}else if(type==4){//半年
				calender.add(Calendar.MONTH, 6);
			}else if(type==5){//年
				calender.add(Calendar.YEAR, 1);
			}

			return sDay.format(calender.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 获取骑行卡截止日期
	 * @param type
	 * @return
	 */
	public static String getRideCardEndDate(Integer type){
		/**在最新的时间上累加*/
		Calendar calender = Calendar.getInstance();
		if(type==0){//日
			calender.add(Calendar.DATE, 1);
		}else if(type==1){//周
			calender.add(Calendar.DATE, 7);
		}else if(type==2){//月
			calender.add(Calendar.MONTH, 1);
		}else if(type==3){//季
			calender.add(Calendar.MONTH, 3);
		}else if(type==4){//季
			calender.add(Calendar.MONTH, 6);
		}else if(type==5){//年
			calender.add(Calendar.YEAR, 1);
		}else if (type==6){//半月
			calender.add(Calendar.DATE, 15);
		}

		return sDay.format(calender.getTime());
	}


	/**获取和当前的分钟差*/
	public static String getMinBefore(Integer minutes){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Long time=System.currentTimeMillis()-minutes*60*1000;

			return s.format(new Date(time));
		}catch(Exception e){
			e.printStackTrace();
		}

		return null;
	}

	public static String addMinutes(String startTime,Integer minutes){
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try{
			Long time=StringToDateTime(startTime).getTime()+minutes*60*1000;
			return s.format(new Date(time));
		}catch(Exception e){
			e.printStackTrace();
		}

		return null;
	}



	private static String[] parsePatterns = {"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm"};

	public static boolean isDateString(String string){
		if (string == null) {
			return false;
		}
		try {
			Date date= DateUtils.parseDate(string, parsePatterns);
            return date != null;
		} catch (Exception e) {
			return false;
		}
	}

	/**获取两个时间差*/
	public static int getTimeDF(String time1, String time2) {
		SimpleDateFormat s = new SimpleDateFormat(forString);
		int time = 0;
		try {
			time = (int) ((s.parse(time2).getTime() - s.parse(time1).getTime()) / 1000);
		} catch (ParseException e) {
			log.error("时间转换出错", e);
		}
		return time;
	}

	public static Integer getMinuteInput(String startTime,Integer minuteInput,Integer stopTime,String endTime){
		//如果用户没有输入，则直接返回0
		if(minuteInput==null){
			return 0;
		}
		SimpleDateFormat s = new SimpleDateFormat(forString);
		try {
			Date oldTime = s.parse(startTime);
			Date nowTime=s.parse(endTime);
			long inter=nowTime.getTime()-oldTime.getTime()-stopTime*1000;//两个时间的时间差
			//计算有多少个分钟
			long minuteInputMills=minuteInput*60*1000;//分钟转化为毫秒
			//计算有多少个分钟
			int num=(int) (inter/minuteInputMills);
			if(inter%minuteInputMills>0){
				num++;
			}
			return num;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * TODO 后期待改善
	 * 获取当前时间
	 *
	 * @return 当前时间
	 */
	public static Calendar getNow() {
		return Calendar.getInstance();
	}


	/**
	 * 获取当前时间
	 *
	 * @return 当前时间
	 */
	public static Date getCurrentTime() {
		return Calendar.getInstance().getTime();
	}

	public static Date getCurrentTimeMinus(Integer second) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.SECOND, second);
		return cal.getTime();
	}

	/**
	 * 获取指定日期格式日期
	 *
	 * @param pattern 日期格式
	 * @return 格式日期
	 */
	public static String getFormatTime(String pattern) {
		Date time = Calendar.getInstance().getTime();
		return DateFormatUtils.format(time, pattern);
	}

	public static void main(String[] args) {
		System.out.println(getDaformatyyyyMMddHHmmss(new Date()));
	}
}
